import React, { useEffect, useState } from "react";
import { reqLoginInfo } from "../../api/index";
import storageUtils from "../../utils/storageUtils";
import memoryUtils from "../../utils/memoryUtils";
import "./header.scss";
import { useNavigate } from "react-router-dom";
import { authReq } from "../../api/httpReq";

export default function Header() {
	const [userData, setUserData] = useState({
		name: "",
		flag: null,
	});
	const navigate = useNavigate();

	useEffect(() => {
		//?保存用户登录信息到内存中
		reqLoginInfo().then((res) => {
			if (!storageUtils.getUser().adminName) {
			}
			const temp = { adminId: 1674887, adminName: "黄柏超", type: 2 };
			memoryUtils.user = res.data;
			//?保存用户登录信息到localstorage
			storageUtils.saveUser(res.data);
			/* memoryUtils.user = temp;
			storageUtils.saveUser(temp); */
			console.log(storageUtils.getUser().adminName);
		});
		setUserData({
			name: storageUtils.getUser().adminName,
			flag: 1,
		});
	}, []);

	const { name } = userData;
	return (
		<div className="header">
			<div className="header-left">
				<h1>邮问必答重邮学习发展中心</h1>
				<span>后台管理系统</span>
			</div>
			<div className="header-right">
				<span>{name}</span>
				<span> | </span>
				<a href="https://ids.cqupt.edu.cn/authserver/logout">退出</a>
			</div>
		</div>
	);
}
